﻿// JavaScript File

//var gmapOffsetX = 0.0006;
//var gmapOffsetY = 0.0004;
//var marker = null;
var c = 0;
var t;
var timer_is_on = 0;
//var PointArray = [];
////var MachineId;
//var nPoints = 0;
var smart_marker;
var last_marker;
var detail_marker;
var last_detail_marker;

var detailsPoints = [];
//var nTicks = 0;
var TotalTicks = 0;



function RefreshMap()
{
    gmap.clearOverlays();
    AddFieldLayer();

}
/*
Add Google Map at start.
*/
function GLoadMap(strLocation) {
    if (GBrowserIsCompatible()) 
    {
        var i = 0; 
        var options={draggableCursor:"auto",draggingCursor:"move"};
        gmap = new GMap2(document.getElementById("map"),options);
        
        var zoom_level=15;
        if(arguments[1]) zoom_level=arguments[1];
        if (strLocation.length>0)
        {
            var lat = strLocation.split(",")[0];
            var lnt = strLocation.split(",")[1];
            gmap.setCenter(new GLatLng(parseFloat(lat),parseFloat(lnt)), zoom_level);
        }
        else
        {
            //gmap.setCenter(new GLatLng(48.113950, 127.105640), zoom_level);
            gmap.setCenter(new GLatLng(45.72711, 126.76170), zoom_level); 
        }
        var largeMapControl=new GLargeMapControl();
        var mapTypeControl = new GMapTypeControl();
        gmap.addControl(largeMapControl);//, topLeft);
        gmap.addControl(new GScaleControl());
        gmap.addControl(mapTypeControl);
        
        GEvent.addListener(gmap,"zoomend",function(oldLevel, newLevel){
            //alert(MAP_TYPE);
            if(MAP_TYPE == "GMAP" || MAP_TYPE == "GIMAGE") return;
            
            gmap.clearOverlays();
            
            if(MAP_TYPE = "IMSMAP")
                AddFieldLayer();
             });
  }
}




function createPolyline(points, map)
{
    //var newpoint = new GPoint(
    
    var newPoints = new Array();
    
    var nPoints = points.length;
    
    for (var i=0;i<nPoints;i++)
    {
        newPoints[i] = new GLatLng(points[i].lat() + gmapOffsetY ,points[i].lng() + gmapOffsetX);
    }
    
    var polyline = new GPolyline(newPoints, "#0000FF", 5);
    map.addOverlay(polyline);
}

/*
**通过point信息对地图上面进行标注，显示相应点上的标注信息
*/
function createMarker2(point, map, myHtml, txtTitle) {
    var newpoint = new GLatLng(point.lat() + gmapOffsetY, point.lng() + gmapOffsetX);

    var tinyIcon = new GIcon();
    tinyIcon.image = "images/smart_marker.png";
    tinyIcon.iconSize = new GSize(42, 42);
    tinyIcon.iconAnchor = new GPoint(21, 21);
    tinyIcon.infoWindowAnchor = new GPoint(1, 1);

    markerOptions = { title: txtTitle, icon: tinyIcon };

    var marker = new GMarker(newpoint, markerOptions);
    GEvent.addListener(marker, "click", function() {
        marker.openInfoWindowHtml(myHtml);
        $("#txtMachineCode").val(txtTitle);
    });
    map.addOverlay(marker);
}



//function addMarker() {
//    
//    //setTimeout(addMarker, 3000);
//    var t = setTimeout("alertMsg()", 3000);
//}

//function addMarker() {
//    gmap.addOverlay(marker);
//}

function doTimer(Points) {
    //PointArray = Points;
    //MachineId = machineId;
    //if (!timer_is_on) {
    //    timer_is_on = 1;
    var nPoints = Points.length;
    var InitPointArray = [];
    
    for (var i = 0; i < nPoints; i++) {
        //InitPointArray[i] = new GLatLng(Points[nPoints - i - 1].lat(), Points[nPoints - i - 1].lng());
        InitPointArray[i] = new GLatLng(Points[i].lat(), Points[i ].lng());
    }

    var minLength = 10000;
    var tempPolyline;
    for (var i = 0; i < (nPoints - 1); i++) {
        tempPolyline = new GPolyline([InitPointArray[i], InitPointArray[i + 1]], "#ff0000", 10);
        if (minLength > tempPolyline.getLength())
            minLength = tempPolyline.getLength();

    }
    var tickLength = minLength / 2;

    //var detailsPoints = [];
    var nTicks = 0;
    //var TotalTicks = 0;
    for (var i = 0; i < (nPoints - 1); i++) {
        tempPolyline = new GPolyline([InitPointArray[i], InitPointArray[i + 1]], "#ff0000", 10);
        nTicks = tempPolyline.getLength() / tickLength;
        if (parseInt(nTicks) != nTicks) {
            nTicks = Math.floor(nTicks) + 1;
        }

        LatLength = InitPointArray[i + 1].lat() - InitPointArray[i].lat();
        LngLength = InitPointArray[i + 1].lng() - InitPointArray[i].lng();
        for (var j = 0; j < nTicks - 1; j++) {
            detailsPoints[TotalTicks + j] = new GLatLng(InitPointArray[i].lat() + j * LatLength / nTicks, InitPointArray[i].lng() + j * LngLength / nTicks);
        }
        detailsPoints[TotalTicks + nTicks - 1] = new GLatLng(InitPointArray[i + 1].lat(), InitPointArray[i + 1].lng());

        TotalTicks += nTicks;


    }
    
    
    
    createMarker1();
    // }

}


//removeOverlay(overlay:GOverlay)
function createMarker1() {

    var newpoint;
    var pointMac;





    //if (c < nPoints) {
    if(c <TotalTicks ){
        if (smart_marker != null && smart_marker != undefined) {
            last_marker = smart_marker;
            gmap.removeOverlay(last_marker);
        }
        //newpoint = new GLatLng(InitPointArray[c].lat() + gmapOffsetY, InitPointArray[c].lng() + gmapOffsetX);
        newpoint = new GLatLng(detailsPoints[c].lat() + gmapOffsetY, detailsPoints[c].lng() + gmapOffsetX);
        
        var tinyIcon = new GIcon();
        tinyIcon.image = "images/smart_marker.png";
        tinyIcon.iconSize = new GSize(42, 42);
        tinyIcon.iconAnchor = new GPoint(21, 21);
        tinyIcon.infoWindowAnchor = new GPoint(1, 1);

        markerOptions = {icon: tinyIcon };

        smart_marker = new GMarker(newpoint, markerOptions);

        gmap.addOverlay(smart_marker);
        c++;

        t = window.setTimeout("createMarker1()", 20);

    }
    else {
        gmap.removeOverlay(smart_marker);
        
        t = clearTimeout();
        c = 0;
        detailsPoints = [];
        TotalTicks = 0;
    
    }
    

    //}

    
    //var newpoint = new GLatLng(point.lat() + gmapOffsetY, point.lng() + gmapOffsetX);




}

function createMarker(point, map, myHtml, txtTitle) {
    if (detail_marker != null && detail_marker != undefined) {
        last_detail_marker = detail_marker;
        gmap.removeOverlay(last_detail_marker);
    }

    
    var newpoint = new GLatLng(point.lat() + gmapOffsetY, point.lng() + gmapOffsetX);

    var tinyIcon = new GIcon();
    tinyIcon.image = "images/smart_marker.png";
    tinyIcon.iconSize = new GSize(42, 42);
    tinyIcon.iconAnchor = new GPoint(21, 21);
    tinyIcon.infoWindowAnchor = new GPoint(1, 1);

    markerOptions = { title: txtTitle, icon: tinyIcon };

    detail_marker = new GMarker(newpoint, markerOptions);
    GEvent.addListener(detail_marker, "click", function() {
        detail_marker.openInfoWindowHtml(myHtml);
        $("#txtMachineCode").val(txtTitle);
    });
    map.addOverlay(detail_marker);
    //last_detail_marker = detail_marker;
}



/*
** 依据地点信息文件cities.xml，对地图进行标注
*/
function markCity(map,point){
   
	    var myHtml;
        myHtml = "<a href = '#'>" + "Hello" + "</a><br/>";
        createMarker(point, map, myHtml); 
}


var MAGIC_NUMBER=6356752.3142;
var DEG2RAD=0.0174532922519943;
var PI=3.14159267;
function dd2MercMetersLng(p_lng) { 
	return MAGIC_NUMBER*(p_lng*DEG2RAD); 
}
function dd2MercMetersLat(p_lat) {
	if (p_lat >= 85) p_lat=85;
	if (p_lat <= -85) p_lat=-85;
	return MAGIC_NUMBER*Math.log(Math.tan(((p_lat*DEG2RAD)+(PI/2)) /2));
}

